
package com.jcl.recruit.core.utils;

import com.jcl.core.exception.SystemException;
import com.jcl.core.utils.EncryptUtils;
import com.jcl.core.constants.MimeTypeConstants;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.core.web.utils.RequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Date;

public class DownloadFilesUtils {

    /**
     * Logger for this class
     */
    private static final Logger logger              = LoggerFactory
                                                           .getLogger(DownloadFilesUtils.class);

    public static final String Content_Disposition = "Content-Disposition";

    public static final String attachment_filename = "attachment;filename=";

    public static final String DEFAUL_FILE_NAME    = "download";

    private static final String CACHE_CONTROL = "Cache-Control";

    private static final String LAST_MODIFIED = "Last-Modified";

    private static final String NO_CACHE      = "no-cache";

    private static final String EXPIRES       = "Expires";

    private static final String E_TAG         = "ETag";

    public static void downloadImg(byte[] img) {

        downloadImg(img,false);
    }

    public static void downloadImg(byte[] img, boolean needCache) {

        downloadImg(img, -1, -1, needCache, null);
    }

    public static void downloadImg(byte[] img, Date lastModifyDate) {

        downloadImg(img, -1, -1, true, lastModifyDate);
    }

    public static void downloadImg(byte[] img, int width, int height, boolean needCache) {

        downloadImg(img, width, height, needCache, null);
    }

    public static void downloadImg(byte[] img, int width, int height, Date lastModifyDate) {

        downloadImg(img, width, height, true, lastModifyDate);
    }

    public static void downloadImg(byte[] img, int width, int height, boolean needCache, Date lastModifyDate) throws SystemException {

        OutputStream out = null;
        try {

            if (img != null && img.length > 0) {
                HttpServletResponse response = RequestUtils.getHttpServletResponse();
                response.setContentType(MimeTypeConstants.MIMETYPE_JPEG);
                if (lastModifyDate != null) {
                    response.setHeader(CACHE_CONTROL, "public");
                    response.setDateHeader(LAST_MODIFIED, lastModifyDate.getTime());
                } else {
                    response.setHeader(CACHE_CONTROL, needCache ? "max-age=" + String
                            .valueOf(Integer.MAX_VALUE) : NO_CACHE);
                }
                response.setHeader(E_TAG, EncryptUtils.getMD5(img));
                out = response.getOutputStream();

                if (width > 0 && height > 0) {
                    ByteArrayInputStream bis = new ByteArrayInputStream(img);
                    BufferedImage origImg = ImageIO.read(bis);
                    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                    result.getGraphics().drawImage(origImg, 0, 0, width, height, null);
                    //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    //encoder.encode(result);
                } else {
                    out.write(img);
                }
                out.flush();
                out.close();
                out = null;
                response.flushBuffer();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e.getMessage(), e);
        }
    }

    /**
     * 改变图片大小
     * @param img
     * @param width
     * @param height
     * @return
     */
    public static byte[] resizeImg(byte[] img, int width, int height) {
        try {
            if (img == null) {
                return null;
            }
            ByteArrayInputStream bis = new ByteArrayInputStream(img);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            BufferedImage origImg = ImageIO.read(bis);
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            result.getGraphics().drawImage(origImg, 0, 0, width, height, null);
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            //encoder.encode(result);

            return bos.toByteArray();
        } catch (Exception e) {
            return null;
        }
    }


}
